package com.firebird.app.kotlin.stock.trend.view

import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.activity.*

/**
 * 自定义底部通用菜单控件
 * @author firebird
 * @date   2019年10月24日
 */
class FooterMenuView : LinearLayoutCompat {
    private var mContext:Context?=null
    private var currentMenu = 0
    private var home: LinearLayoutCompat? = null
    private var quotation: LinearLayoutCompat? = null
    private var optional: LinearLayoutCompat? = null
    private var forecast: LinearLayoutCompat? = null
    private var set: LinearLayoutCompat? = null
    private var bt_home: AppCompatButton? = null
    private var bt_quotation: AppCompatButton? = null
    private var bt_optional: AppCompatButton? = null
    private var bt_forecast: AppCompatButton? = null
    private var bt_set: AppCompatButton? = null
    private var tv_home: AppCompatTextView? = null
    private var tv_quotation: AppCompatTextView? = null
    private var tv_optional: AppCompatTextView? = null
    private var tv_forecast: AppCompatTextView? = null
    private var tv_set: AppCompatTextView? = null

    constructor(context: Context?) : super(context, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        this.mContext = context
        View.inflate(context, R.layout.layout_menu_footer, this)
        home = findViewById<View>(R.id.menu_home) as LinearLayoutCompat
        quotation = findViewById<View>(R.id.menu_quotation) as LinearLayoutCompat
        optional = findViewById<View>(R.id.menu_optional) as LinearLayoutCompat
        forecast = findViewById<View>(R.id.menu_forecast) as LinearLayoutCompat
        set = findViewById<View>(R.id.menu_set) as LinearLayoutCompat
        bt_home = findViewById<View>(R.id.menu_home_bt) as AppCompatButton
        bt_quotation = findViewById<View>(R.id.menu_quotation_bt) as AppCompatButton
        bt_optional = findViewById<View>(R.id.menu_optional_bt) as AppCompatButton
        bt_forecast = findViewById<View>(R.id.menu_forecast_bt) as AppCompatButton
        bt_set = findViewById<View>(R.id.menu_set_bt) as AppCompatButton
        tv_home = findViewById<View>(R.id.menu_home_tv) as AppCompatTextView
        tv_quotation = findViewById<View>(R.id.menu_quotation_tv) as AppCompatTextView
        tv_optional = findViewById<View>(R.id.menu_optional_tv) as AppCompatTextView
        tv_forecast = findViewById<View>(R.id.menu_forecast_tv) as AppCompatTextView
        tv_set = findViewById<View>(R.id.menu_set_tv) as AppCompatTextView
    }

    /**
     * 设置当前选中的菜单项
     * @param currentMenu
     */
    fun setCurrentMenu(currentMenu: Int) {
        this.currentMenu = currentMenu
        val colorRed = resources.getColor(R.color.menu_red)
        val colorWhith = resources.getColor(R.color.white)
        bt_home!!.isSelected = false
        bt_quotation!!.isSelected = false
        bt_optional!!.isSelected = false
        bt_forecast!!.isSelected = false
        bt_set!!.isSelected = false
        tv_home!!.setTextColor(colorWhith)
        tv_quotation!!.setTextColor(colorWhith)
        tv_optional!!.setTextColor(colorWhith)
        tv_forecast!!.setTextColor(colorWhith)
        tv_set!!.setTextColor(colorWhith)
        when (currentMenu) {
            R.id.menu_home -> {
                bt_home!!.isSelected = true
                tv_home!!.setTextColor(colorRed)
            }
            R.id.menu_quotation -> {
                bt_quotation!!.isSelected = true
                tv_quotation!!.setTextColor(colorRed)
            }
            R.id.menu_optional -> {
                bt_optional!!.isSelected = true
                tv_optional!!.setTextColor(colorRed)
            }
            R.id.menu_forecast -> {
                bt_forecast!!.isSelected = true
                tv_forecast!!.setTextColor(colorRed)
            }
            R.id.menu_set -> {
                bt_set!!.isSelected = true
                tv_set!!.setTextColor(colorRed)
            }
            else -> {
                bt_home!!.isSelected = true
                tv_home!!.setTextColor(colorRed)
            }
        }
    }

    /**
     * 设置菜单点击事件监听器
     * @param onClickListener
     */
    override fun setOnClickListener(onClickListener: OnClickListener?) {
    }

    /**
     * 设置菜单触摸事件监听器
     * @param onClickListener
     */
    override fun setOnTouchListener(listener: OnTouchListener) {
        home!!.setOnTouchListener(listener)
        quotation!!.setOnTouchListener(listener)
        optional!!.setOnTouchListener(listener)
        forecast!!.setOnTouchListener(listener)
        set!!.setOnTouchListener(listener)
    }

    /**
     * 设置菜单触摸事件监听器
     * @param onClickListener
     */
    inner class MenuOnTouchListener : OnTouchListener {
        override fun onTouch(v: View, event: MotionEvent): Boolean {
            val color = resources.getColor(R.color.menu_red)
            var intent: Intent? = null
            when (v.id) {
                R.id.menu_home -> {
                    bt_home!!.isSelected = true
                    tv_home!!.setTextColor(color)
                    if (currentMenu != R.id.menu_home && event.action == MotionEvent.ACTION_UP) {
                        intent = Intent(context, HomeActivity::class.java)
                        context!!.startActivity(intent)
                    }
                }
                R.id.menu_quotation -> {
                    bt_quotation!!.isSelected = true
                    tv_quotation!!.setTextColor(color)
                    if (currentMenu != R.id.menu_quotation && event.action == MotionEvent.ACTION_UP) {
                        intent = Intent(context, QuotationActivity::class.java)
                        context!!.startActivity(intent)
                    }
                }
                R.id.menu_optional -> {
                    bt_optional!!.isSelected = true
                    tv_optional!!.setTextColor(color)
                    if (currentMenu != R.id.menu_optional && event.action == MotionEvent.ACTION_UP) {
                        intent =  Intent(context, OptionalActivity::class.java)
					    context!!.startActivity(intent)
                        //Toast.makeText(context, "Zixuan", Toast.LENGTH_SHORT).show()
                    }
                }
                R.id.menu_forecast -> {
                    bt_forecast!!.isSelected = true
                    tv_forecast!!.setTextColor(color)
                    if (currentMenu != R.id.menu_forecast && event.action == MotionEvent.ACTION_UP) {
                        intent = Intent(context, ForecastActivity::class.java)
                        context!!.startActivity(intent)
                    }
                }
                R.id.menu_set -> {
                    bt_set!!.isSelected = true
                    tv_set!!.setTextColor(color)
                    if (currentMenu != R.id.menu_set && event.action == MotionEvent.ACTION_UP) {
                        intent = Intent(context, SetActivity::class.java)
                        context!!.startActivity(intent)
                    }
                }
            }
            return true
        }
    }

    /**
     * 内部类用于处理底部菜单点击后页面跳转
     * @author ljk
     */
    inner class MenuOnClickListener : OnClickListener {
        override fun onClick(v: View) {
            var intent: Intent? = null
            when (v.id) {
                R.id.menu_home -> if (currentMenu != R.id.menu_home) {
                    intent = Intent(context, HomeActivity::class.java)
                    context!!.startActivity(intent)
                }
                R.id.menu_quotation -> if (currentMenu != R.id.menu_quotation) {
                    intent = Intent(context, QuotationActivity::class.java)
                    context!!.startActivity(intent)
                }
                R.id.menu_optional -> if (currentMenu != R.id.menu_optional) {
                    intent=Intent(context,OptionalActivity::class.java)
                    //Toast.makeText(context, "optional", Toast.LENGTH_SHORT).show()
                    context!!.startActivity(intent)
                }
                R.id.menu_forecast -> if (currentMenu != R.id.menu_forecast) {
                    intent = Intent(context, ForecastActivity::class.java)
                    context!!.startActivity(intent)
                }
                R.id.menu_set -> if (currentMenu != R.id.menu_set) {
                    intent = Intent(context, SetActivity::class.java)
                    context!!.startActivity(intent)
                }
            }
        }
    }
}